package com.weavernorth.retailExperienceAward.utils;

import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.retailExperienceAward.utils
 * @date: 2020/6/1 11:02
 * @description:
 */
public class convertDateUtil {
    /***
     * 获取当前日期上个月的年,月
     * @return
     */
    public static String previousMonth(){
        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(new Date());//设置当前日期
        calendar.add(Calendar.MONTH, -1);
        String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()).toString().split("-");
        Integer year = Integer.parseInt(strNow[0]);
        Integer monthInt = Integer.parseInt(strNow[1]);
        String month="0";
        if (monthInt<10){
            month="0"+Util.null2String(monthInt);
        }else{
            month=Util.null2String(monthInt);
        }
        return year+month;
    }
    /***
     * 获取当前日期前N个月的年,月
     * @return
     */
    public static String previousMonth(Integer n){
        Calendar calendar = Calendar.getInstance();//日历对象
        calendar.setTime(new Date());//设置当前日期
        calendar.add(Calendar.MONTH, n);
        String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()).toString().split("-");
        Integer year = Integer.parseInt(strNow[0]);
        Integer monthInt = Integer.parseInt(strNow[1]);
        String month="0";
        if (monthInt<10){
            month="0"+Util.null2String(monthInt);
        }else{
            month=Util.null2String(monthInt);
        }
        return year+month;
    }
    /***
     * 获取当前考核周期的上个周期
     * @return
     */
    public static String previousMonth(String khzq){
        String year="";
        String month="";
        try{
           if (khzq.length()>0){
               year=khzq.substring(0,4);
               Integer monthInt = Integer.parseInt(khzq.substring(4,6));
               if (monthInt>1&&monthInt<=10){
                   month="0"+Util.null2String(monthInt-1);
               }else if(monthInt>10){
                   month=Util.null2String(monthInt-1);
               }else{
                   year=Util.null2String(Integer.parseInt(year)-1);
                   month="12";
               }
           }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return year+month;
    }

    /***
     * 取某年某月最后一天的日期
     * @param year
     * @param month
     * @return
     */
    public static String LastDayOfMonth(int year,int month)
    {
        Calendar calendar = Calendar.getInstance();
        //设置年
        calendar.set(Calendar.YEAR,year);
        //设置月
        calendar.set(Calendar.MONTH,month-1);
        //获取某月最大天数
        BaseBean bb=new BaseBean();
        bb.writeLog("获取某月最大天数");
        int lastDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        bb.writeLog("某月最大天数为。。"+lastDay);
        //设置日历中月份的最大天数
        bb.writeLog("设置日历中月份的最大天数。。");
        calendar.set(Calendar.DAY_OF_MONTH,lastDay);
        //格式化日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth  = sdf.format(calendar.getTime());
        bb.writeLog("格式化日期。。"+lastDayOfMonth+"");
        return lastDayOfMonth;
    }

    public static int getMonthByjd(int jd){
        int result=0;
        switch (jd){
            case 0:
                result=3;
                break;
            case 1:
                result=6;
                break;
            case 2:
                result=9;
                break;
            case 3:
                result=12;
                break;
        }
        return result;
    }
    /***
     * 获取当前日期
     * @return
     */
    public static String getYearMonthDay()
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());//设置当前日期
        //格式化日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String yearMonthDay  = sdf.format(calendar.getTime());
        return yearMonthDay;
    }

    /***
     * 获取当前时间
     * @return
     */
    public static String getHourMs()
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());//设置当前日期
        //格式化日期
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
        String HourMs  = sdf.format(calendar.getTime());
        return HourMs;
    }

    /***
     * 获取某年某月最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

    /***
     * 获取当前日期上个月的最后一天
     * @param strDate:yyyy-MM-dd
     * @return
     */
    public static String getLastDate(String strDate){
        int year= Integer.parseInt(strDate.substring(0,4));
        int month= Integer.parseInt(strDate.substring(5,7));
        if (month<2){
            year=year-1;
            month=12;
        }else{
            month=month-1;
        }
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }
}
